/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package res.elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import plar.core.Element;
import plar.core.Action;
import plar.core.Common;
import plar.core.Parameters;
import plar.core.NoGravity;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.collision.*;

public class Platform extends Element  {
class AdjustPlayer implements Action {
		public AdjustPlayer() {

		}

		public Parameters call(Parameters parameters)

		{

			Element me = (Element) parameters.get("me");
			String subAction = (String) parameters.get("subAction");
			ArrayList<Element> collisions = (ArrayList<Element>) me.getProperty("collisionList");
			
			if (subAction == "main" ) {
				Vec2 currentPosition = me.getPosition();
				Vec2 centralPosition =(Vec2) me.getProperty("centralPosition");
				Vec2  s =(Vec2) me.getProperty("sensibility");
				
				
				Vec2 delta = new Vec2(
				-currentPosition.x+centralPosition.x,
				-currentPosition.y+centralPosition.y
				);
				if(Math.abs(delta.x)>s.x || Math.abs(delta.y)>s.y )
				{
			/*		Common.info(3,"platform exit fom the center");
					Vec2 factor =(Vec2) me.getProperty("returnFactor");
					delta.x=factor.x*delta.x;
					delta.y=factor.y*delta.y;
					me.setSpeed(delta);*/
					Vec2 pfactor =(Vec2) me.getProperty("returnPositionFactor");
					Vec2 sfactor =(Vec2) me.getProperty("returnSpeedFactor");
					me.applyForce(new Vec2
					(me.body.getMass()*(pfactor.x*delta.x-sfactor.x*getSpeed().x),
					 me.body.getMass()*(pfactor.y*delta.y-sfactor.y*getSpeed().y))
					);
					
						
				}
				
			}
			return null;
		}
	}
	public Platform() {
	
		stdinfo.put("subAction","main");
		setProperty("isStatic", false);
		setProperty("isRunnable", true);
		setProperty("friction",10f);
		setProperty("denisity", 5);
		setProperty("sensibility", new Vec2(1f,0.5f));
		setProperty("centralPosition", new Vec2(0,0));
		setProperty("returnPositionFactor", new Vec2(50f,1.1f));
		setProperty("returnSpeedFactor", new Vec2(100f,1f));

		setStaticSprite("res/elements/elevator.png");
		setProperty("canCollide", true);
		
	
		actions.addAction("adjust", new AdjustPlayer());
		actions.addAction("nogravity", new NoGravity());
	}
}
 
